

var _HEAD_GLOBAL_DATA = {

    is_customer : true,     //判断是否处于客户权限下

    query_info : {

        receiver_type : 'customer',

        receiver_id : '',

        is_viewed : '0'

    },

    product_types : [],     //本地存储的用户购买的机器类型

    audio_src : '../../../audio/new_info.mp3',     //音频地址

    new_order_infos : [],    //新订单提醒数组

    new_order_ids : [],     //新订单id数组

    wemall_only : false,     //判断是否只购买了微商城

    is_show_ratio: false,   // 显示分成比例的权限

    is_show_supply_price: false,    //

    is_show_qrcode_url: false      // 是否显示二维码

};


//消息
var remindResource = new Resource( API_LIST.MESSAGE_REMIND , Vue.http );

//服务通用
var remindCreateResource = new Resource( API_LIST.REMIND_CREATE , Vue.http );

!function orderRemind(){

    var head_vm = new Vue({

        el : '#order_remind',

        data : _HEAD_GLOBAL_DATA,

        created : function (){

            var self = this;

            self.initData()
                .then(function( res ){

                    if( res ){

                        self.orderInfoGet();

                        setInterval( self.orderInfoGet , 30000);

                    }

                })

        },

        methods : {
            //更新订单查看状态
            updateViewStatus : function updateViewStatus(){

                var self = this;

                var params = {

                    is_viewed : 1,

                    receiver_id : self.query_info.receiver_id,

                    receiver_type : 'customer'

                };

                remindResource.update( 'all_viewed' , params )
                            .then(function (res) {

                                if (parseInt(res.data.code) === 0) {

                                    if( !self.wemall_only ){

                                        if(!self.isOrderPage()){

                                            self.new_order_infos.forEach(function(item){

                                                self.new_order_ids.push(item.messageable_id);

                                            });

                                            location.href = API_LIST.TO_ALLORDER;

                                        }

                                    }else {

                                        if(!self.isOrderPage()) {

                                            self.new_order_infos.forEach(function(item){

                                                self.new_order_ids.push(item.messageable_id);

                                            });

                                            location.href = API_LIST.TO_WEMALL_ORDER;

                                        }

                                    }

                                } else {

                                    alert(res.data.error);

                                }

                            })
                            .catch(function (err) {

                                console.log(err);

                                alert('网络异常！');

                            })

            },
            //获取订单消息
            orderInfoGet : function orderInfoGet(){

                var self = this;

                remindResource.index( self.query_info )
                            .then(function (res) {

                                if (parseInt(res.data.code) === 0) {

                                    if( res.data.notifications.length > 0 ){

                                        self.new_order_infos = res.data.notifications;

                                        self.$refs.infoAudio.play();

                                        if( self.isOrderPage() ){

                                            self.updateViewStatus();

                                        }

                                    }

                                }

                            })
                            .catch(function (err) {

                                console.log(err);

                                // alert('网络异常！');

                            })

            },
            //判断当前页面是否是订单查看页面
            isOrderPage : function isOrderPage(){

                var url_info = location.href.split('/').pop().split('-');

                if( url_info.indexOf('order') == -1 || url_info.indexOf('query') == -1 ){

                    return false;

                }else {

                    return true;

                }

            },
            //初始化所需数据
            initData : function initData(){

                var self = this;

                return remindCreateResource.index()
                                    .then(function (res) {

                                        if (parseInt(res.data.code) === 0) {

                                            if( parseInt(res.data.subordinates.admin_level) === 2 ){

                                                self.is_customer = true;

                                                self.query_info.receiver_id = parseInt(res.data.subordinates.customer_id);

                                                self.product_types = res.data.purchase_type_ids;

                                                self.is_show_ratio = res.data.is_show_ratio;

                                                self.is_show_supply_price =  res.data.is_show_supply_price;

                                                self.is_show_qrcode_url = res.data.is_show_qrcode_url;

                                                if( self.product_types.indexOf(1) != -1 ){

                                                    if( self.product_types.indexOf(2) == -1 &&
                                                        self.product_types.indexOf(3) == -1 &&
                                                        self.product_types.indexOf(4) == -1 &&
                                                        self.product_types.indexOf(5) == -1){

                                                        self.wemall_only = true;

                                                    }

                                                }

                                                return true;

                                            }else {

                                                self.is_customer = false;

                                                return false;

                                            }

                                        } else {

                                            alert(res.data.error);

                                        }

                                    })
                                    .catch(function (err) {

                                        console.log(err);

                                        alert('网络异常！');

                                    })

            }

        }

    })

}();

//切割网址的函数
function urlInfoSplit(url){

    var infos = {};

    // TODO destruct assignment
    var url_query_arr = url.split("#").pop().split("?");

    url_query_arr.pop().split("&").forEach( function(v){

        infos[v.split("=")[0]] = v.split("=")[1];

    });

    return infos;

}